package com.InteInfo.server.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.apache.log4j.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.InteInfo.common.MD5;
import com.InteInfo.dao.IBaseDao;
import com.InteInfo.model.User;
import com.InteInfo.server.IUserService;

@Service("userServiceImpl")
public class UserServiceImpl implements IUserService {

	private static final Logger logger = Logger
			.getLogger(UserServiceImpl.class);

	private IBaseDao<User> baseDao;

	public IBaseDao<User> getBaseDao() {
		return baseDao;
	}

	@Autowired
	public void setBaseDao(IBaseDao<User> baseDao) {
		this.baseDao = baseDao;
	}

	public void save() {
		logger.info("测试Spring+Hibernate");
		MD5 md5 = new MD5();

		User user = new User();
		user.setUserid(UUID.randomUUID().toString());
		user.setUserpassword(md5.getMDThirtytwo("123456"));
		user.setUsername("张三");
		user.setCreatedate(new Date());
		this.baseDao.save(user);

	}

	public User getUser(User user) {
		return null;
	}

	public User getUserByStaffID(String staffid) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("staffid", staffid);
		User user = baseDao.get("from User t where t.staffid = :staffid", params);
		if (user != null) {
			return user;
		} else {
			return null;
		}

	}

	public Boolean checkUser(String staffid, String userpassword) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("staffid", staffid);
		params.put("userpassword", userpassword);
		User user = baseDao.get("from User t where t.staffid = :staffid and t.userpassword = :userpassword", params);
		if (user != null) {
			return true;
		} else {
			return false;
		}
	}

}
